package com.canbot.u05.activity.customvoice;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.bean.AnswerBean;
import com.canbot.u05.activity.customvoice.bean.QuestionBean;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by liu.heng
 * 自定义对话动作方式选择
 */

public class CustomActionSetActivity extends BaseActivityNoTitle {

        private ImageView default_action_image;

        private ImageView custom_action_image;

        private final int ADD_ACTION = 0;

        private final int CHANGE_ACTION = 1;

        private int type;

        private String mActionId = "";// 动作ID

        private int answerType;//回答的方式 TTS:1 音频:0

        private String answer_text = "";//答案

        private String question_Str = "";//TTS文本或音频路径

        private String image_Path = "";//图片路径

        private Dialog dialogLoading;

        private AnswerBean mAnswerBean;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_custom_action_set);
                default_action_image = (ImageView) findViewById(R.id.default_action_image);
                custom_action_image = (ImageView) findViewById(R.id.custom_action_image);
                final Intent intent = getIntent();
                type = intent.getIntExtra("type", -1);
                if (type == CHANGE_ACTION) {
                        question_Str = intent.getStringExtra("question_Str");
                        mAnswerBean = JSON.parseObject(intent.getStringExtra("answer"), AnswerBean.class);
                        mActionId = mAnswerBean.getActionid();
                }
                else {
                        question_Str = intent.getStringExtra("question_Str");
                        answer_text = intent.getStringExtra("answer_Str");
                        image_Path = intent.getStringExtra("image_Path");
                        answerType = intent.getIntExtra("answerType", 0);
                        //默认选择默认动作
                        default_action_image.setVisibility(View.VISIBLE);
                        custom_action_image.setVisibility(View.GONE);
                        Log.e("------>", image_Path + "");
                }
        }

        @Override
        protected void onResume() {
                super.onResume();
                if (!TextUtils.isEmpty(mActionId)) {
                        default_action_image.setVisibility(View.GONE);
                        custom_action_image.setVisibility(View.VISIBLE);
                }
                else {
                        default_action_image.setVisibility(View.VISIBLE);
                        custom_action_image.setVisibility(View.GONE);
                }
        }

        /*动作设置选择*/
        public void buttonEvent(View view) {
                switch (view.getId()) {
                        case R.id.default_action://默认动作
                                mActionId = "";
                                default_action_image.setVisibility(View.VISIBLE);
                                custom_action_image.setVisibility(View.GONE);
                                break;
                        case R.id.custom_action://指定动作
                                default_action_image.setVisibility(View.GONE);
                                custom_action_image.setVisibility(View.VISIBLE);
                                if (type == CHANGE_ACTION) {
                                        startActivityForResult(new Intent(this, CustomActionActivity.class)
                                                .putExtra("action", mActionId)
                                                .putExtra("type",1), 10001);
                                }
                                else {
                                        startActivityForResult(new Intent(this, CustomActionActivity.class), 10001);
                                }
                                break;
                        default:
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == 10001 && resultCode == Activity.RESULT_OK) {
                        mActionId = data.getStringExtra("action_ID");
                }
                else {
                        return;
                }
        }

        /*返回*/
        public void back(View view) {
                finish();
        }

        /*完成*/
        public void nextStep(View view) {

                if (type == ADD_ACTION) {

                        final QuestionBean questionBean = new QuestionBean();
                        questionBean.setQuestion(question_Str);
                        questionBean.setType(1);//自定义设置type为1

                        final AnswerBean answerBean = new AnswerBean();
                        answerBean.setActionid(mActionId);
                        answerBean.setType(answerType);
                        answerBean.setImgpath(image_Path);
                        if (answerType == 0) {//通过回答type给TTS文本和音频路径赋值
                                answerBean.setSoundpath(answer_text);
                        }
                        else {
                                answerBean.setTtstext(answer_text);
                        }

                        Log.e("------>", questionBean + "&&&" + answerBean);
                        final List<QuestionBean> questionBeanList = new ArrayList<>();
                        questionBeanList.add(questionBean);

                        final List<AnswerBean> answerBeansList = new ArrayList<>();
                        answerBeansList.add(answerBean);

                        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                        dialogLoading.show();
                        final HashMap<String, Object> hasMap = new HashMap<>();
                        hasMap.put("type", "0");
                        hasMap.put("questions", JSON.toJSONString(questionBeanList));
                        hasMap.put("answers", JSON.toJSONString(answerBeansList));

                        OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "dialogcustom", hasMap, new OkHttpUtils.OkHttpCallBack() {
                                @Override
                                public void onSuccess(String result) {
                                        final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);

                                        final String msg;

                                        if (strMsgBean.getRc() != 0) {
                                                msg = strMsgBean.getMsg();
                                        }
                                        else {
                                                msg = CustomActionSetActivity.this.getString(R.string.custom_success);
                                        }
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        ToastTools.newInstance(getApplicationContext()).showText(msg);
                                                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.CUSTOM_DIALOG_PAYH + "u05_dzcg_001.wav");
                                                        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "30");
                                                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14009");
                                                        Intent intent = new Intent(CustomActionSetActivity.this, CustomDialogListActivity.class);
                                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                        startActivity(intent);
                                                        finish();
                                                }
                                        });

                                }

                                @Override
                                public void onFailure() {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.network_error));
                                                }
                                        });
                                }
                        });

                }
                else if (type == CHANGE_ACTION) {
                        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                        dialogLoading.show();
                        final HashMap<String, Object> hasMap = new HashMap<>();
                        hasMap.put("type", "4");
                        hasMap.put("question", question_Str);
                        hasMap.put("actionid", mActionId);

                        OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "dialogcustom", hasMap, new OkHttpUtils.OkHttpCallBack() {
                                @Override
                                public void onSuccess(String result) {
                                        final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);

                                        final String msg;

                                        if (strMsgBean.getRc() != 0) {
                                                msg = strMsgBean.getMsg();
                                        }
                                        else {
                                                msg = CustomActionSetActivity.this.getString(R.string.update_successfully);
                                        }
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        ToastTools.newInstance(getApplicationContext()).showText(msg);
                                                        dialogLoading.dismiss();
                                                        finish();
                                                }
                                        });
                                }

                                @Override
                                public void onFailure() {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.network_error));
                                                }
                                        });
                                }
                        });
                }
        }
}
